import {
    getSTSToken
} from "@/api/modules/auth"
import COS from "cos-js-sdk-v5";

const state = {
    cos: null
}

const mutations = {
    INITIALIZECOS(state, cos) {
        state.cos = cos
    }
}

const actions = {
    // 初始化cos
    InitializeCos({
        commit
    }) {
        console.log('initial')


        let cos = new COS({

            getAuthorization: function(options, callback) {
                getSTSToken().then(res => {
                    console.log(res)

                    if (res.status == 200) {
                        const {
                            sessionToken,
                            tmpSecretId,
                            tmpSecretKey
                        } =
                        res.data.credentials;
                        console.log('datas', sessionToken, tmpSecretId, tmpSecretKey)

                        try {
                            // 在回调中返回凭证信息

                            callback({
                                TmpSecretId: tmpSecretId,
                                TmpSecretKey: tmpSecretKey,
                                SecurityToken: sessionToken,
                                StartTime: res.data.startTime,
                                ExpiredTime: res.data.expiredTime,
                                ScopeLimit: true,
                            });
                        } catch (error) {
                            console.log(error)
                        }

                    }
                });
            }



        })
        console.log("cos", cos)
        commit("INITIALIZECOS", cos)


    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}